package com.zhoufeng.udp.service;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import com.zjhbkj.xinfen.commom.Global;
import com.zjhbkj.xinfen.model.RcvComsModel;

public class UDPServer1 {
	public static void main(String[] args) throws IOException {

		//while (true) {
			DatagramSocket server = new DatagramSocket(5050);
			// byte[] recvBuf = new byte[100];
			byte[] arrayOfByte = new byte[Global.COMMAND_LENGTH];
			byte[] msg = arrayOfByte;
			DatagramPacket recvPacket = new DatagramPacket(arrayOfByte, arrayOfByte.length);
			System.out.println("service start!");
			server.receive(recvPacket);
			System.out.println("recv end");

			RcvComsModel localRcvComsModel = new RcvComsModel();
			byte[] arrayOfByte2 = recvPacket.getData();
			boolean bool = localRcvComsModel.receiveCommand(arrayOfByte2);
			int j = msg.length;
			recvPacket.setLength(j);
			String sendStr = "Hello ! I'm Server";
			byte[] sendBuf;
			sendBuf = sendStr.getBytes();

			InetAddress addr = recvPacket.getAddress();
			int port = recvPacket.getPort();
			
			System.out.println(localRcvComsModel.getCommand1());

			//DatagramPacket sendPacket = new DatagramPacket(sendBuf, sendBuf.length, addr, port);
			server.send(recvPacket);
			server.close();
		//}

	}
}
